/**
 *  公文审核列表
 */

$(document).ready(function () {

    //初始化表格
    function initTable(data){
        var $datatable = $('#datatable-checkbox');
        $datatable.dataTable({
            data:data,
            "createdRow": function ( row, data, index ) {
                $('td', row).css("text-align","center").css("vertical-align","middle");
            },
            columns:[
                {"sTitle": "发送时间"},
                {"sTitle": "公文标题"},
                {"sTitle": "公文号"},
                {"sTitle": "公文文种"},     //3
                {"sTitle": "紧急程度"},
                {"sTitle": "接受部门"},
                {"sTitle": "审核状态"},
            ],
            order:[
                [0, "desc"]
            ],
            columnDefs: [
                { orderable: false, targets: [0] },
            ],
            bLengthChange:false
        } );

    }

    // 获取全部公文审核列表
    $.ajax({
        type: 'POST',
        url: '../document/list/review',
        contentType: "application/json",
        dataType: 'JSON',
        data:JSON.stringify({}),        // 不做分页处理
        success: function (json,status) {
            var datas = new Array();
            var arr = new Array();
            var docs = json.data.documents;
            // console.log(docs);
            for(var i = 0;i<docs.length;i++){
                arr = [];
                var already_approval = docs[i].already_approval;
                var $bg = "bg-green";
                arr[0] = docs[i].sent_time;
                arr[1] = docs[i].title;
                arr[2] = docs[i].number;
                arr[3] = docs[i].type;
                arr[4] = docs[i].serious_type;
                if(docs[i].serious_type == "NOT_SERIOUS"){
                    arr[4] = "不紧急";
                }else if(docs[i].serious_type == "SERIOUS"){
                    arr[4] = "紧急";
                }
                arr[5] = docs[i].receive;

                if(already_approval == false){
                    arr[6] = "<button type='button' class='btn btn-warning' onclick='ReviewDocument("+ docs[i].id +")'>" + "点击审核</button>"
                }else if(already_approval == true){
                    arr[6] = "<button type='button' class='btn btn-success' onclick='ReviewDocument("+ docs[i].id + ")'>公文查看</button><br/>";
                    arr[6] += "审核已通过";
                }
                datas[i] = arr;
            }
            initTable(datas);
            if(json.err_msg != null){
                alert(json.err_msg);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log(errorThrown);
        },
    });
});

function ReviewDocument(doc_id) {
    var url = "../document/my_exam_content.html?doc_id=" + doc_id;
    window.location.href = url;
}